from celery_tasks.main import celery_app
from celery import shared_task
from django.conf import settings
import requests
import json
import logging
logger = logging.getLogger('django')
@shared_task(name='send_dingding')
def send_dingding(Msg,s_type=2):

    headers = {'Content-Type': 'application/json;charset=utf-8'}
    if s_type == 1:
        isAtAll = True #@所有人
    else:
        isAtAll = False #@指定手机号的人
    json_text = {
        "msgtype": "text",
        "at": {
            "atMobiles": [
                "18806207447"
            ],
            "isAtAll": isAtAll
        },
        "text": {
            "content": Msg
        }
    }
    try:

        response = requests.post(settings.DING_WEBHOOK_URL, json.dumps(json_text), headers=headers)
        if response.status_code ==200:
            return response.content
        else:
            return '钉钉消息发送失败'



    except BaseException as e:
        logger.error(e)
        return '钉钉消息发送异常：%s'%e

